import { FILE_UPLOAD_URL, SESSION_KEY } from '../utils/constant.js'

export const fileUpload = (file, callback) => {
	const token = uni.getStorageSync(SESSION_KEY)
	uni.uploadFile({
		url: FILE_UPLOAD_URL + '/common/upload',
		filePath: file,
		name: 'file',
		header: {
			Authorization: `wx ${token}`
		},
		success (uploadFileRes) {
			const { statusCode, data } = uploadFileRes
			const { code, msg, fileName, url } = JSON.parse(data)
			if (statusCode === 200 && code === 200) {
				if (msg) {
					uni.showToast({
						title: msg
					})
				}
				callback({ fileName, url })
			}
		}
	})
}